home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / TabPanel.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  11.8 KB  |  467 lines

  1. /*
  2.  Copyright (c) 1995, 1996 Connect! Corporation, Inc. All Rights Reserved.
  3.  Source code usage restricted as defined in Connect! Widgets License Agreement
  4. */
  5.  
  6. package symantec.itools.awt;
  7.  
  8. import java.awt.*;
  9. import java.util.Vector;
  10. import java.lang.Boolean;
  11.  
  12. //    01/29/97    RKM    Integrated Tim's changes to preserve the current index
  13.  
  14. /**
  15.  * TabPanel is a Panel extension which provides for
  16.  * a tabbed dialog effect. It will automatically manage
  17.  * swapping panels when a tab is shown/activated.
  18.  * It can be used directly or extended.
  19.  * When extending from TabPanel be sure to super()
  20.  * during construction and to super.handleEvent(evt) from
  21.  * handleEvent if you override it.
  22.  *
  23.  * @author Scott Fauerbach
  24.  */
  25. public class TabPanel extends BaseTabbedPanel
  26. {
  27.     Vector vPanels;
  28.  
  29.     String[] labels=null;
  30.  
  31.     boolean bOsHack;
  32.  
  33.     /**
  34.      * Constructs a TabPanel with tabs on top, rounded
  35.      */
  36.     public TabPanel()
  37.     {
  38.         this(TOP, ROUNDED);
  39.         }
  40.  
  41.     /**
  42.      * Obsolete. Use public TabPanel(int tabsPostion, int tabsStyle)
  43.      * Constructs a TabPanel.
  44.      */
  45.     public TabPanel(boolean bTabsOnTop)
  46.     {
  47.         this(bTabsOnTop ? TOP : BOTTOM, bTabsOnTop ? ROUNDED : SQUARE);
  48.     }
  49.  
  50.     /**
  51.      * Constructs a TabPanel.
  52.      * @param tabsPosition a constant indicating TOP or BOTTOM
  53.      * @param tabsStyle a constant indicating ROUNDED or SQUARE
  54.      */
  55.     public TabPanel(int tabsPostion, int tabsStyle)
  56.     {
  57.         super(tabsPostion, tabsStyle);
  58.         vPanels = new Vector();
  59.         String sOS = System.getProperty("os.name");
  60.         if (sOS.equals("Windows 95"))
  61.             bOsHack = true;
  62.         else
  63.             bOsHack = false;
  64.     }
  65.  
  66.     /**
  67.      * Adds a user panel which will be shown when a tab is activated.
  68.      * @param sLabel the tab label
  69.      * @param bEnabled enable the tab or not
  70.      * @param panel the panel to associate with the tab
  71.      * @return the zero-relative index of the newly added tab panel
  72.      */
  73.     public int addTabPanel(String sLabel, boolean bEnabled, Component panel)
  74.     {
  75.         vPanels.addElement(panel);
  76.  
  77.         return addTab(sLabel, bEnabled);
  78.     }
  79.  
  80.     /**
  81.      * Returns the zero-relative index of the currently active panel.
  82.      * @return the currently shown panel or -1 if none are shown
  83.      * @see setCurrentPanelNdx
  84.      */
  85.     public int getCurrentPanelNdx() 
  86.     {
  87.         return curIndex;
  88.     }
  89.  
  90.     /**
  91.      * Shows the specified panel.
  92.      * @param index the zero-relative index of the panel to show.
  93.      * @see getCurrentPanelNdx
  94.      */ 
  95.     public void setCurrentPanelNdx(int index) {
  96.         showTabPanel(index);
  97.  
  98.         // If we aren't designing, set current index even if the panel hasn't been
  99.         // added yet (we'll switch to it in add())
  100.         if (! symantec.beans.Beans.isDesignTime())
  101.             curIndex = index;
  102.     }
  103.  
  104.     /**
  105.      * This standard Java AWT Container method adds a Component to
  106.      * this container. It is overriden here to ensure the Component
  107.      * is added to the end.
  108.      */
  109.     public Component add(Component comp) { return add(comp,-1); }
  110.  
  111.     private String createDefaultLabel(int i) {
  112.         String name="tab - ";
  113.         name += String.valueOf(i);
  114.         return name;
  115.     }
  116.  
  117.     /**
  118.      * This standard Java AWT Container method adds a component to this 
  119.      * Container at the given component index.
  120.      * @param comp component to add
  121.      * @param pos zero-relative index at which to add the component or -1 for end
  122.      * @return added component
  123.      * @see #remove
  124.      */
  125.     public synchronized Component add(Component comp, int pos) {
  126.        int newIndex = addTabPanel(createDefaultLabel(vPanels.size()),true,comp);
  127.        
  128.        // If this is the panel that we've set to be the default, or we're designing,
  129.        // go ahead and switch to the new panel.
  130.        if (newIndex == curIndex || symantec.beans.Beans.isDesignTime())
  131.            showTabPanel(newIndex);
  132.        updatePanelLabels();
  133.        return comp;
  134.     }
  135.  
  136.     /**
  137.      * This standard Java AWT method is NOT USED.
  138.      * @param s component name for the LayoutManager
  139.      * @param c component to add to panel
  140.      * @return component parameter
  141.      * @see #remove
  142.      */
  143.     public synchronized Component add(String name, Component comp) { return comp; }
  144.  
  145.     /**
  146.      * Remove a component from this panel.
  147.      * @param comp component to remove from panel
  148.      * @see #add
  149.      */
  150.     public synchronized void remove(Component comp) 
  151.     {
  152.       int i=getPanelTabIndex(comp);
  153.  
  154.       if (countTabs()==1) 
  155.           removeAllTabPanels();
  156.       else  {
  157.           if (i==0)
  158.          showTabPanel(1);
  159.           else
  160.          showTabPanel(i-1);
  161.           removeTabPanel(i);
  162.       }
  163.     }
  164.  
  165.     /**
  166.      * Sets the tab labels associated with the current panels.
  167.      * @param sLabels an array of tab labels for the current panels
  168.      * @see getPanelLabels
  169.      */
  170.     public void setPanelLabels(String[] sLabels)
  171.     {
  172.         labels=sLabels;
  173.         updatePanelLabels();
  174.     }
  175.  
  176.     /**
  177.      * Gets the current tab labels associated with the current panels.
  178.      * @return an array of tab labels for the current panels
  179.      * @see setPanelLabels
  180.      */
  181.     public String[] getPanelLabels() 
  182.     {
  183.         return labels;
  184.     }
  185.  
  186.     /**
  187.      * This routine re-sets all the tab labels using the latest String array
  188.      * provided in setPanelLabels().
  189.      * @see setPanelLabels
  190.      */
  191.     public void updatePanelLabels() 
  192.     {
  193.         try {
  194.         for (int i=0;i<vPanels.size();i++) {
  195.         String newlabel;
  196.         if (labels!=null) {
  197.         try {
  198.             newlabel=labels[i];
  199.         } catch (ArrayIndexOutOfBoundsException e){
  200.             newlabel=createDefaultLabel(i);
  201.         }
  202.         }else 
  203.             newlabel=createDefaultLabel(i);
  204.         setLabel(newlabel,i);
  205.         }
  206.         } catch(Throwable thr) {}
  207.     }
  208.  
  209.     /**
  210.      * Puts the tabs on the top or bottom of the dialog.
  211.      * @param bTabsOnBottom if true the tabs are placed at the bottom of the
  212.      * dialog, if false on top
  213.      * @see getTabsOnBottom
  214.      */
  215.     public void setTabsOnBottom(boolean bTabsOnBottom)
  216.     {
  217.             setTabsInfo(bTabsOnBottom ? BOTTOM : TOP, bTabsOnBottom ? SQUARE : ROUNDED);
  218.         layout();
  219.     }
  220.  
  221.     /**
  222.      * Gets whether the tabs are at the bottom of the dialog.
  223.      * @return true if the tabs are at the bottom of the dialog, false if
  224.      * they are at the top
  225.      * @see setTabsOnBottom
  226.      */
  227.     public boolean getTabsOnBottom() 
  228.     {
  229.          return getTabsPosition()==TOP ? false : true;
  230.     }
  231.  
  232.  
  233.     /**
  234.      * Replaces the user panel at the index specified.
  235.      * Allows ability to change tab label and/or tab panel.
  236.      * If it is desired to only change the label, use the base class
  237.      * BaseTabbedPanel's method setTab(String sLabel, boolean bEnabled, int index).
  238.      * @param sLabel the new tab label
  239.      * @param bEnabled enable the tab or not
  240.      * @param panel the new panel
  241.      * @see BaseTabbedPanel#setTab
  242.      * @see getTabPanel
  243.      */
  244.     public synchronized void setTabPanel(String sLabel, boolean bEnabled, Component panel, int index)
  245.     {
  246.         if ((index < 0) || (index >= vPanels.size()))
  247.             return;
  248.  
  249.         if (index == currentTabIndex() && !bEnabled)
  250.             return;
  251.  
  252.         try
  253.         {
  254.             vPanels.setElementAt(panel, index);
  255.             setTab(sLabel, bEnabled, index);
  256.         }
  257.         catch (ArrayIndexOutOfBoundsException e) {}
  258.     }
  259.  
  260.     /**
  261.      * Gets the Panel for the tab at the given index.
  262.      * @param index zero-relative index of the tab to show
  263.      * @return returns the Panel associated with the tab
  264.      * @see setTabPanel
  265.      */
  266.     public synchronized Component getTabPanel(int index)
  267.     {
  268.         if ((index < 0) || (index >= vPanels.size()))
  269.             return null;
  270.  
  271.         Component p = null;
  272.         try
  273.         {
  274.             p =  (Component)vPanels.elementAt(index);
  275.         }
  276.         catch (ArrayIndexOutOfBoundsException e) {}
  277.  
  278.         return p;
  279.     }
  280.  
  281.     /**
  282.      * Gets the index for a specific Panel.
  283.      * @param panel the current panel to get the index of
  284.      * @return the zero-relative index of the panel or -1 if it is not found
  285.      */
  286.     public synchronized int getPanelTabIndex(Component panel)
  287.     {
  288.         return vPanels.indexOf(panel);
  289.     }
  290.  
  291.     /**
  292.      * Shows and activates a tab and its tab panel at the given index. 
  293.      * The tab position must be enabled.
  294.      * @param index zero-relative index of the tab to show
  295.      * @see enableTabPanel
  296.      */
  297.     public synchronized void showTabPanel(int index)
  298.     {
  299.         if ( tabIsEnabled(index) )
  300.         {
  301.             try
  302.             {
  303.                 Component p = (Component) vPanels.elementAt(index);
  304.                 showTab(index);
  305. //                if (bOsHack && p != null)
  306. //                {
  307. //                    p.hide();
  308. //                    setPanel(p);
  309. //                    p.show();
  310. //                }
  311. //                else
  312.                     showPanel(p);
  313.             }
  314.             catch (ArrayIndexOutOfBoundsException e) {}
  315.         }
  316.     }
  317.  
  318.     /**
  319.      * Enables (or disables) a tab and its tab panel at the given index.
  320.      * The currently active tab cannot be disabled.
  321.      * @param bEnable true to enable, false to disable
  322.      * @param index zero-relative index of the tab
  323.      */
  324.     public synchronized void enableTabPanel(boolean bEnable, int index)
  325.     {
  326.         if ((index < 0) || (index >= vPanels.size()) || index == curIndex)
  327.             return;
  328.  
  329.         enableTab(bEnable, index);
  330.     }
  331.  
  332.     /**
  333.      * Removes a tab and its tab panel at the given index.
  334.      * The current active tab cannot be removed.
  335.      * @param index zero-relative index of the tab
  336.      */
  337.     public synchronized void removeTabPanel(int index)
  338.     {
  339.         if ((index < 0) || (index >= vPanels.size()) || index == curIndex)
  340.             return;
  341.  
  342.         try
  343.         {
  344.             Component p = (Component) vPanels.elementAt(index);
  345.             super.remove(p);
  346.             vPanels.removeElementAt(index);
  347.         }
  348.         catch (ArrayIndexOutOfBoundsException e) {}
  349.  
  350.         removeTab(index);
  351.     }
  352.  
  353.     /**
  354.      * Removes all tab panels, clearing the TabPanel entirely.
  355.      */
  356.     public synchronized void removeAllTabPanels()
  357.     {
  358.         vPanels = new Vector();
  359.         curIndex = -1;
  360.         removeAllTabs();
  361.     }
  362.  
  363.     /**
  364.      * Gets the number of tabs in the TabPanel.
  365.      * @return the number of tabs currently in the TabPanel
  366.      */
  367.     public int countTabs()
  368.     {
  369.         return vPanels.size();
  370.     }
  371.  
  372.     /**
  373.      * This standard routine is called by the Java AWT to handle events.
  374.      * @param evt the event to handle
  375.      * @return true if the event was handled an no further action is needed,
  376.      * false to pass the event to this component's parent
  377.      */
  378.     public boolean handleEvent(Event evt)
  379.     {
  380.         switch (evt.id)
  381.         {
  382.             case Event.ACTION_EVENT:
  383.                 if (evt.target instanceof TabPanel)
  384.                 {
  385.                     if (evt.target == this)
  386.                         showTabPanel(currentTabIndex());
  387.                 }
  388.                 break;
  389.         }
  390.         return super.handleEvent(evt);
  391.     }
  392.  
  393.     /**
  394.      * Returns the recommended dimensions to properly display this component.
  395.      */
  396.     public Dimension preferredSize()
  397.     {
  398.         Component pan = null;
  399.         Dimension d = null;
  400.         Dimension p = size();
  401.         int s = vPanels.size();
  402.  
  403.         Insets insets = insets();
  404.         p.width -= (insets.left + insets.right);
  405.         p.height -= (insets.top + insets.bottom);
  406.  
  407.         if (p.width < 0)
  408.             p.width = 0;
  409.  
  410.         if (p.height < 0)
  411.             p.height = 0;
  412.  
  413.         for (int x = 0; x < s; x++)
  414.         {
  415.             pan = (Component) vPanels.elementAt(x);
  416.             if (pan != null)
  417.             {
  418.                 d = pan.minimumSize();
  419.                 if (d.width > p.width)
  420.                     p.width = d.width;
  421.                 if (d.height > p.height)
  422.                     p.height = d.height;
  423.                 d = pan.preferredSize();
  424.                 if (d.width > p.width)
  425.                     p.width = d.width;
  426.                 if (d.height > p.height)
  427.                     p.height = d.height;
  428.             }
  429.         }
  430.  
  431.         p.width += (insets.left + insets.right);
  432.         p.height += (insets.top + insets.bottom);
  433.  
  434.         return p;
  435.     }
  436.  
  437.     /**
  438.      * Returns the minimum dimensions to properly display this component.
  439.      */
  440.     public Dimension minimumSize()
  441.     {
  442.         Component pan = null;
  443.         Dimension d = null;
  444.         Dimension m = new Dimension(0,0);
  445.         int s = vPanels.size();
  446.  
  447.         for (int x = 0; x < s; x++)
  448.         {
  449.             pan = (Component) vPanels.elementAt(x);
  450.             if (pan != null)
  451.             {
  452.                 d = pan.minimumSize();
  453.                 if (d.width > m.width)
  454.                     m.width = d.width;
  455.                 if (d.height > m.height)
  456.                     m.height = d.height;
  457.             }
  458.         }
  459.  
  460.         Insets insets = insets();
  461.         m.width += (insets.left + insets.right);
  462.         m.height += (insets.top + insets.bottom);
  463.  
  464.         return m;
  465.     }
  466. }
  467.